package com.example.testq;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/request")
public class RequestControler {

    @RequestMapping("/r1")
    public String r1(MultipartFile file) {
        System.out.println(file.getOriginalFilename());
        return file.getOriginalFilename().toString();

    }

    @RequestMapping("/r2")
    public String r2(UserInfo userInfo) {
        return userInfo.toString();
    }

    @RequestMapping("/r3")
    public String r3(@RequestParam("q") String key) {
        return "返回值:" + key;
    }

    @RequestMapping("/r4")
    public String r4(String[] arr) {
        return "返回值：:" + Arrays.toString(arr);
    }

    @RequestMapping("/r5")
    public String r5(@RequestParam List<String> arr) {
        return "返回值:" + arr.toString();
    }

    @RequestMapping("/r6")
    public String r6(@RequestBody UserInfo userInfo) {
        return userInfo.toString();

    }

    @RequestMapping("/r7/arkcai/{q}")
    public String r7(@PathVariable String q) {
        return q;
    }

    @RequestMapping("/r8/arkcai/{q}/{f}")
    public String r8(@PathVariable String q, @PathVariable("f") String list) {
        return q + "," + list;
    }

    @RequestMapping("r9")
    public String r9(MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:\\截图\\" + file.getOriginalFilename()));
        return "上传成功";

    }


    @RequestMapping("/r10")
    public String r10(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + ":" + cookie.getValue());
            }
        }
        return "获取成功";
    }

    @RequestMapping("/r11")
    public String r11(@CookieValue("age") String age) {
        System.out.println(age);
        return "获取的Cookie：" + age;
    }

    @RequestMapping("/r12")
    public String r12(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("name", "zhangsan");
        session.setAttribute("password", "123456");
        return "设置成功";

    }

    @RequestMapping("/r13")
    public String r13(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            String  name = (String) session.getAttribute("name");
            String  password = (String) session.getAttribute("password");
            System.out.println(name + " " + password);
            return name + " " + password;
        }
        return null;
    }

    @RequestMapping("/r14")
    public String r14(HttpSession session) {
    if (session != null) {
        String name = (String) session.getAttribute("name");
        return name;
    }
    return null;
    }

    @RequestMapping("/r15")
    public String r15(@SessionAttribute("name") String name) {
        return name;
    }

    @RequestMapping("/r16")
    public String r16(HttpServletRequest request) {
        String header = request.getHeader("User-Agent");
        return header;
    }

    @RequestMapping("/r17")
    public String r17(@RequestHeader("Host") String host) {

        return host;
    }

    @RequestMapping("/r18")
    public String r18(HttpServletRequest request) {
//        String header = request.getHeader("User-Agent");
//        return header;

        String header = request.getHeader("Cookie");
        return header;
    }

}
